package com.canxi.earn.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.canxi.common.core.page.TableDataInfo;
import com.canxi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.canxi.earn.domain.bo.CxFunsBo;
import com.canxi.earn.domain.vo.CxFunsVo;
import com.canxi.earn.domain.CxFuns;
import com.canxi.earn.mapper.CxFunsMapper;
import com.canxi.earn.service.ICxFunsService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import com.canxi.cxcommon.utils.PageUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.hutool.core.util.StrUtil;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;

/**
 * 功能列Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-04
 */
@RequiredArgsConstructor
@Service
public class CxFunsServiceImpl  extends JoinServiceImpl<CxFunsMapper, CxFuns>  implements ICxFunsService {

    private final CxFunsMapper baseMapper;

    @Override
    public TableDataInfo<CxFunsVo> queryCxFunsList(CxFunsBo cxFunsBo) {

//        String apiKey = "a675f121a673dfb7c0527e9087d1840a";
//        String apiUrl = "http://v.juhe.cn/joke/content/list.php";
//
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("key", apiKey);
//        map.put("sort", "desc");
//        map.put("page", "20");
//        map.put("pagesize", "");
//        map.put("time", System.currentTimeMillis()/1000+"");
//
//        String s = HttpUtil.get(apiUrl, map);
//        JSONObject object = JSONUtil.parseObj(s);
//


        return PageUtils.buildDataInfo(this.baseMapper.queryCxFunsList(PageUtils.buildPage(), BeanUtil.toBean(cxFunsBo, CxFunsVo.class)));
    }

    @Override
    public List<CxFunsVo> queryCxFunsAll(CxFunsBo cxFunsBo) {
        return this.baseMapper.queryCxFunsList(BeanUtil.toBean(cxFunsBo, CxFunsVo.class));
    }



    /**
     * 查询功能列
     */
    @Override
    public CxFunsVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询功能列列表
     */
    @Override
    public List<CxFunsVo> queryList(CxFunsBo bo) {
        JoinLambdaWrapper<CxFuns> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private JoinLambdaWrapper<CxFuns> buildQueryWrapper(CxFunsBo bo) {
        Map<String, Object> params = bo.getParams();
        JoinLambdaWrapper<CxFuns> lqw = joinLambdaQueryWrapper();
        lqw.eq(StrUtil.isNotBlank(bo.getIconOssId()), CxFuns::getIconOssId, bo.getIconOssId());
        lqw.like(StrUtil.isNotBlank(bo.getName()), CxFuns::getName, bo.getName());
        lqw.eq(StrUtil.isNotBlank(bo.getToType()), CxFuns::getToType, bo.getToType());
        lqw.eq(bo.getAmount() != null, CxFuns::getAmount, bo.getAmount());
        lqw.eq(StrUtil.isNotBlank(bo.getType()), CxFuns::getType, bo.getType());
        lqw.eq(StrUtil.isNotBlank(bo.getStatus()), CxFuns::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增功能列
     */
    @Override
    public Boolean insertByBo(CxFunsBo bo) {
        CxFuns add = BeanUtil.toBean(bo, CxFuns.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改功能列
     */
    @Override
    public Boolean updateByBo(CxFunsBo bo) {
        CxFuns update = BeanUtil.toBean(bo, CxFuns.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CxFuns entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除功能列
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 获取APP端的功能列表
     * @param cxFunsBo
     * @return
     */
    @Override
    public Map<String,List<CxFunsVo>> queryAppCxFunsAll(CxFunsBo cxFunsBo) {
        if(StrUtil.isBlank(cxFunsBo.getStatus())){
            cxFunsBo.setStatus("1");
        }
        CxFunsVo vo = BeanUtil.toBean(cxFunsBo, CxFunsVo.class);
        List<CxFunsVo> vos = baseMapper.queryAppCxFunsList(vo);
        Map<String, List<CxFunsVo>> collect = vos.stream().collect(Collectors.groupingBy(CxFunsVo::getType));
        return collect;
    }

}
